package com.health.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.demo.common.vo.Result;
import com.health.demo.common.vo.Search;
import com.health.demo.entity.Drug;
import com.health.demo.dao.DrugDao;
import com.health.demo.entity.User;
import com.health.demo.service.DrugService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 洛璃依
 * @since 2022-12-07 01:57:02
 */
@Service
public class DrugServiceImpl extends ServiceImpl<DrugDao, Drug> implements DrugService {

    @Resource
    DrugDao drugDao;

    @Override
    public Result<Drug> addDrug(Drug drug) {
        int num = drugDao.insert(drug);
        if (num>0){
            return Result.ok(drug);
        }else {
            return Result.error("插入数据错误！");
        }
    }

    @Override
    public Result<Drug> deleteDrug(Integer drugId) {
        int num = drugDao.deleteById(drugId);
        if (num>0){
            return Result.ok();
        }else {
            return Result.error("delete error ! ");
        }
    }

    @Override
    public Result<Drug> updateDrug(Drug drug) {
        int num = drugDao.updateById(drug);
        if (num>0){
            return Result.ok(drug);
        }else {
            return Result.error("update error ！");
        }
    }

    @Override
    public Drug selectDrugById(Integer drugId) {
        return drugDao.selectById(drugId);
    }

    @Override
    public Page<Drug> selectDrugPage(Search search) {
        search.initSearch();
        Page<Drug> page = new Page<>(search.getCurrentPage(),search.getPageSize());
        QueryWrapper<Drug> drugQueryWrapper = new QueryWrapper<>();

//        drugQueryWrapper.eq(search.getKeyWord()!=null&&!search.getKeyWord().equals("")
//                ,"drug_type",search.getKeyWord());
        drugQueryWrapper.like(StringUtils.isNotBlank(search.getKeyWord())
                ,"drug_name",search.getKeyWord());

        return drugDao.selectPage(page, drugQueryWrapper);
    }
}
